package com.example.studentdemo.controller;

import com.example.studentdemo.annotation.RequiresRole;
import com.example.studentdemo.common.Result;
import com.example.studentdemo.entity.SubjectStudent;
import com.example.studentdemo.constant.RoleConstant;
import com.example.studentdemo.service.SubjectStudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/subjectStudent")
@Tag(name = "学生课程管理", description = "课程学生关联管理")
public class SubjectStudentController {

    @Resource
    private SubjectStudentService subjectStudentService;

    /**
     * 添加单个学生到课程
     */
    @PostMapping("/add")
    @Operation(summary = "添加单个学生到课程")
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE})
    public Result add(@RequestBody SubjectStudent subjectStudent) {
        subjectStudentService.addStudentToSubject(subjectStudent);
        return Result.success();
    }

    /**
     * 批量添加学生到课程
     */
    @PostMapping("/batchAdd")
    @Operation(summary = "批量添加学生到课程")
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE})
    public Result batchAdd(@RequestParam Integer subjectId, @RequestBody List<Integer> studentIds) {
        subjectStudentService.batchAddStudentsToSubject(subjectId, studentIds);
        return Result.success();
    }

    /**
     * 从课程中移除单个学生
     */
    @DeleteMapping("/remove")
    @Operation(summary = "从课程中移除单个学生")
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE})
    public Result remove(@RequestBody SubjectStudent subjectStudent) {
        subjectStudentService.removeStudentFromSubject(subjectStudent);
        return Result.success();
    }

    /**
     * 批量从课程中移除学生
     */
    @DeleteMapping("/batchRemove")
    @Operation(summary = "批量从课程中移除学生")
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE})
    public Result batchRemove(@RequestParam Integer subjectId, @RequestBody List<Integer> studentIds) {
        subjectStudentService.batchAddStudentsToSubject(subjectId, studentIds);
        return Result.success();
    }

    /**
     * 根据课程ID查询学生
     */
    @GetMapping("/bySubjectId")
    @Operation(summary = "根据课程ID查询学生")
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE})
    public Result getBySubjectId(@RequestParam Integer subjectId) {
        return Result.success(subjectStudentService.selectBySubjectId(subjectId));
    }

    /**
     * 根据学生ID查询课程
     */
    @GetMapping("/byStudentId")
    @Operation(summary = "根据学生ID查询课程")
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE, RoleConstant.STUDENT_ROLE})
    public Result getByStudentId(@RequestParam Integer studentId) {
        return Result.success(subjectStudentService.selectByStudentId(studentId));
    }

    /**
     * 分页查询课程学生关联记录
     */
    @GetMapping("/selectPage")
    @Operation(summary = "分页查询课程学生关联记录")
    @RequiresRole(roles = {RoleConstant.ADMIN_ROLE, RoleConstant.TEACHER_ROLE})
    public Result selectPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, SubjectStudent subjectStudent) {
        return Result.success(subjectStudentService.selectPage(pageNum, pageSize, subjectStudent));
    }
}